﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SES.CMS.BL;
using SES.CMS.DO;

namespace SES.CMS.AdminCP.PageUC
{
    public partial class ucConfig : System.Web.UI.UserControl
    {
        sysConfigDO objConfig = new sysConfigDO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["ConfigID"] != null)
            {
                objConfig.ConfigID = int.Parse(Request.QueryString["ConfigID"].ToString());
                initForm();
            }
        }

        private void initForm()
        {
            objConfig = new sysConfigBL().Select(objConfig);
            txtTitle.Text = objConfig.ConfigName;
            // su dung editor
            if (objConfig.ConfigID == 11 || objConfig.ConfigID == 20 || objConfig.ConfigID == 21 || objConfig.ConfigID == 24)
            {
                txtCKX.Content = objConfig.ConfigValue;
                trPopup.Visible = false;
                trDes.Visible = false;
                trSupport.Visible = true;
            }
            // su dung textbox
            else if (objConfig.ConfigID == 4 || objConfig.ConfigID == 5 || objConfig.ConfigID == 14 || objConfig.ConfigID == 16 || objConfig.ConfigID == 15 || objConfig.ConfigID == 1 || objConfig.ConfigID == 18 || objConfig.ConfigID == 17 || objConfig.ConfigID == 22)
            {
                txtDescription.Text = objConfig.ConfigValue;
                trDes.Visible = true;
                trSupport.Visible = false;
                trPopup.Visible = false;
            }

            else if (objConfig.ConfigID == 19 || objConfig.ConfigID == 23 || objConfig.ConfigID == 25 || objConfig.ConfigID == 26)
            {
                trPopup.Visible = true;
                trDes.Visible = false;
                trSupport.Visible = false;
                if (objConfig.ConfigID == 19)
                    hplImage.NavigateUrl = "/Media/dang-viet-logo.jpg";
                if (objConfig.ConfigID == 23)
                    hplImage.NavigateUrl = "/Media/logo.jpg";
                if (objConfig.ConfigID == 25)
                    hplImage.NavigateUrl = "/Media/footer.jpg";
                if (objConfig.ConfigID == 26)
                    hplImage.NavigateUrl = "/Media/header-dangviet.gif";
            }
            else
            {
                txtCKX.Content = objConfig.ConfigValue;
                trPopup.Visible = false;
                trDes.Visible = false;
                trSupport.Visible = true;
            }
            chkActive.Checked = objConfig.IsActive;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            initObject();
            if (objConfig.ConfigID <= 0)
            {
                new sysConfigBL().Insert(objConfig);
            }
            else
            {
                new sysConfigBL().Update(objConfig);
            }
            Functions.Alert("Cập nhật thành công!", "Default.aspx?Page=ListConfig");
        }

        private void initObject()
        {
            objConfig.ConfigName = txtTitle.Text;
                      // su dung editor
            if (objConfig.ConfigID == 11 || objConfig.ConfigID == 20 || objConfig.ConfigID == 21 || objConfig.ConfigID == 24)
            {
                objConfig.ConfigValue = txtCKX.Content;
            }
            //upload image
            else if (objConfig.ConfigID == 19 || objConfig.ConfigID == 23 || objConfig.ConfigID == 25 || objConfig.ConfigID == 26)
            {
                UploadFile(fuImage);
                objConfig.ConfigValue = "";
            }
           // su dung textbox
            else if (objConfig.ConfigID == 4 || objConfig.ConfigID == 5 || objConfig.ConfigID == 14 || objConfig.ConfigID == 16 || objConfig.ConfigID == 15 || objConfig.ConfigID == 1 || objConfig.ConfigID == 18 || objConfig.ConfigID == 17 || objConfig.ConfigID == 22)
            {
                objConfig.ConfigValue = txtDescription.Text.Trim();
            }
            else
            {
                objConfig.ConfigValue = txtCKX.Content;
            }
            objConfig.IsActive = chkActive.Checked;

        }
        private string UploadFile(FileUpload fulImages)
        {
            if (!string.IsNullOrEmpty(fulImages.FileName))
            {
                string FileName = "dang-viet-logo.jpg";
                if (objConfig.ConfigID == 19)
                    FileName = "dang-viet-logo.jpg";
                if (objConfig.ConfigID == 23)
                    FileName = "logo.jpg";
                if (objConfig.ConfigID == 25)
                    FileName = "footer.jpg";
                if (objConfig.ConfigID == 26)
                    FileName = "header-dangviet.gif";
                string SaveLocation = string.Format("{0}\\{1}", Server.MapPath("~/Media/"), FileName);
                if (System.IO.File.Exists(SaveLocation))
                    System.IO.File.Delete(SaveLocation);
                fulImages.SaveAs(SaveLocation);
                return FileName;
            }
            return string.Empty;
        }
    }
}